//var objId =  getQueryString("objId");
var $;
layui.config({
	base : "js/"
}).use(['form','layer','jquery'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage;
		$ = layui.jquery;
  
	   getPageList();
	   
	   //操作
	$("body").on("click",".users_edit",function(){  //同意
		var _this = $(this);
		var dto = 
		{
			"id":_this.attr("data-id"),
			"status":2
		};
		$.ajax({
			        url: API_URL_HEAD+"refundInfo/update",
			        type: 'post',
			        async: false,
			        data: JSON.stringify(dto),
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
			        	var vo = data.data;
			        	if(data.code == 0)
			        	{
			        	layer.msg('退款成功！', {icon: 1});	
			        	}
			        	getPageList();
			        }
			   });
		
	})
	   //操作
	$("body").on("click",".users_reject",function(){  //拒绝
		var _this = $(this);
		var dto = 
		{
			"id":_this.attr("data-id"),
			"refundType":0,
			"status":3
		};
		$.ajax({
			        url: API_URL_HEAD+"refundInfo/update",
			        type: 'post',
			        async: false,
			        data: JSON.stringify(dto),
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
			        	layer.msg('退款已拒绝！', {icon: 1});
			        	var vo = data.data;
			        	getPageList();
			        }
			   });
	})
    //加载页面数据
	function renderData(data){
		$(".vo_content").empty();
		var dataHtml = '';
		if(data.length != 0){
			for(var i=0;i<data.length;i++){
				var payTypeHtml = ['删除','退款申请','退款完成','拒绝退款'];
				var controlHtml = "";
				if(data[i].status == 1)
				{
					controlHtml+=    '<a class="layui-btn layui-btn-mini users_edit" data-id="'+data[i].id+'"><i class="iconfont icon-edit"></i>退款完成</a>'
					+    '<a class="layui-btn layui-btn-danger layui-btn-mini users_reject" data-id="'+data[i].id+'"><i class="layui-icon">&#xe640;</i> 拒绝</a>'
				}
				dataHtml += '<tr>'
			    	+  '<td><input type="checkbox" name="checked" lay-skin="primary" data-id="'+data[i].id+'" lay-filter="choose"></td>'
			    	+  '<td>'+data[i].refundSn+'</td>'
			    	+  '<td>'+data[i].studentName+'</td>'
			    	+  '<td>'+data[i].refundObjPrice+'</td>'
			    	+  '<td>'+data[i].refundObjName+'</td>'
			    	+  '<td>'+data[i].refundObjNums+'</td>'
			    	+  '<td>'+data[i].totalMoney+'</td>'
			    	+  '<td>'+payTypeHtml[data[i].status]+'</td>'
			    	+  '<td>'+handleNullToNullString(data[i].remark)+'</td>'
			    	+  '<td>'+
					controlHtml
			        +  '</td>'
			    	+'</tr>';
			}
		}else{
			dataHtml = '<tr><td colspan="14">暂无数据</td></tr>';
		}
		$(".vo_content").html(dataHtml);
		form.render();
	}
	
	
	
	function getPageList(){
	var data={
		"searchKey":$(".search_input").val(),
		"refundType":0
	};
	 
	    $.ajax({
        url: API_URL_HEAD+"refundInfo/queryPage",
        type: 'post',
        async: false,
        data :JSON.stringify(data),
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (data) {
        	var list = data.data.list;
        	renderData(list);
        }
    });
}
	form.on("select(deliveryMan)",function(data){
		
		getPageList();
	})
 	$(".addUser").click(function(data){
 		var $checkbox = $('.vo_list tbody input[type="checkbox"][name="checked"]');
		var $checked = $('.vo_list tbody input[type="checkbox"][name="checked"]:checked');
		if(null == $(".deliveryMan").val() || $(".deliveryMan").val() == ""  )
		{
			layer.msg('请选择配送人！', {icon: 0}); 
			return;
		}
		if($checkbox.is(":checked")){
			var index = layer.msg('添加中，请稍候',{icon: 16,time:false,shade:0.8});
					var orderArray = "";
					for(var j=0;j<$checked.length;j++){
            			var orderId = $checked.eq(j).attr("data-id");
            			orderArray= orderArray+orderId+",";
	            	}
					orderArray = orderArray.substring(0,orderArray.length-1);
			//是否添加过信息
 		var dto = {
 			"deliveryId":$(".deliveryMan").val(),
 			"orderIds":orderArray
 		};
	 	$.ajax({
			        url: API_URL_HEAD+"outPutBills/create",
			        type: 'post',
			        async: false,
			        data: JSON.stringify(dto),
			        contentType: 'application/json; charset=utf-8',
			        dataType: 'json',
			        success: function (data) {
			        	if(data.code == 0){
			        		layer.close(index);
			        		layer.msg('修改信息成功！', {icon: 1});
		              		 //刷新父页面
			 				parent.location.reload();
		            	}
			        }
			  });
		}
 		return false;
 	})
 	//全选
	form.on('checkbox(allChoose)', function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		child.each(function(index, item){
			item.checked = data.elem.checked;
		});
		form.render('checkbox');
	});

	//通过判断文章是否全部选中来确定全选按钮是否选中
	form.on("checkbox(choose)",function(data){
		var child = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"])');
		var childChecked = $(data.elem).parents('table').find('tbody input[type="checkbox"]:not([name="show"]):checked')
		if(childChecked.length == child.length){
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = true;
		}else{
			$(data.elem).parents('table').find('thead input#allChoose').get(0).checked = false;
		}
		form.render('checkbox');
	})
//生成select options
	function createSelectOptions(selObj,dataArray,id){
		for(var i in dataArray){
			var every = dataArray[i];
			if(id==undefined){
				$(selObj).append("<option value='"+every["id"]+"'>"+every["deliveryName"]+"</option>");
			}else{
				if(every["id"]==id){
					$(selObj).append("<option value='"+every["id"]+"'  selected=\"selected\">"+every["deliveryName"]+"</option>");
				}else{
					$(selObj).append("<option value='"+every["id"]+"'>"+every["deliveryName"]+"</option>");
				}
			}
			
		}
	}	
})

//格式化时间
function formatTime(_time){
	var date = new Date();
	date.setTime(_time);
    if(_time && _time!=null && _time !="")
    {
    	var year = date.getFullYear();
	    var month = date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
	    var day = date.getDate()<10 ? "0"+date.getDate() : date.getDate();
	    var hour = date.getHours()<10 ? "0"+date.getHours() : date.getHours();
	    var minute = date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
	    return year+"-"+month+"-"+day+" "+hour+":"+minute;
    }else
    {
    	return "";
    }
}
//获取url中的参数
function getQueryString(name) {
  var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 匹配目标参数
  var result = window.location.search.substr(1).match(reg); // 对querystring匹配目标参数
  if (result != null) {
    return decodeURIComponent(result[2]);
  } else {
    return null;
  }
}